<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/merchant-home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索与增加区域 -->
    <el-card class="box-card">
      <!-- 搜索与添加区域 -->
      <el-row :gutter="30">
        <el-col :span="9">
          <el-input
            placeholder="请输入宠物类别"
            v-model="queryInfo.query"
            clearable
            @clear="getProductList()"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getProductList()"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddpage">添加商品</el-button>
        </el-col>
      </el-row>
    </el-card>
    <!-- table表格区域 -->
    <el-table :data="productlist" border stripe>
      <el-table-column label="#" type="index"></el-table-column>
      <el-table-column label="商品名称" prop="name" width="100px"></el-table-column>
      <el-table-column label="商品价格(元)" prop="price" width="110px"></el-table-column>
      <el-table-column label="商品重量" prop="weight" width="90px"></el-table-column>
      <el-table-column label="商品描述" prop="description"></el-table-column>
      <el-table-column label="创建时间" prop="addTime" width="175px">
        <template slot-scope="scope">
          {{ scope.row.addTime | dateFormat }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="130px">
        <template slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            @click="showEditDialog(scope.row)"
          ></el-button>
          <!-- 删除按钮 -->
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="removeById(scope.row.id)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 查询参数对象
      queryInfo: {
        query: '',
        productId: null,
        pagenum: 1,
        pagesize: 5
      },
      productlist: [],
      total: 0,
      addProductDialogVisible: false
    }
  },
  created() {
    this.queryInfo.merchantId = window.localStorage.getItem('merchantID')
    this.getProductList()
  },
  methods: {
    // 获取宠物商品数据
    async getProductList() {
      const { data: res } = await this.$axios.get('/product/search',
      { params: this.queryInfo })
      if (res.code !== 200) {
        return this.$message.error('获取数据失败！ ')
      }
      this.$message.success('获取商品列表成功')
      this.productlist = res.data.product
      this.total = res.data.total
    },
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getProductList()
    },
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getProductList()
    },
    async removeById(id) {
      console.log(id)
      const confirmResult = await this.$confirm('此操作将永久删除该商品, 是否继续?', '删除商品', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err => err)
        if (confirmResult !== 'confirm') {
          return this.$message.info('已经取消删除')
        }
        const { data: res } = await this.$axios.delete('/product/delete/' + id)
        console.log(res)
        if (res.code !== 200) {
          return this.$message.error('删除失败！ ')
        }
        this.$message.success('删除成功！ ')
        this.getProductList()
    },
    goAddpage() {
      this.$router.push('/merchant-home/add')
    },
    showEditDialog(row) {
      this.$router.push({ path: '/merchant-home/modify', query: { product: row } })
    }
  }
}
</script>

<style>

</style>
